package org.abacus.abs.reflect;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 *
 * @author chaoyang.li
 *
 */
public class TypeHelper {

	/**
	 *
	 * @param typeOfObject
	 * @return
	 */
	public Type[] getTypes(ParameterizedType typeOfObject) {
		if (null == typeOfObject) {
			return null;
		}
		return typeOfObject.getActualTypeArguments();
	}

	/**
	 *
	 * @param typeOfObject
	 * @return
	 */
	public Type getRawType(ParameterizedType typeOfObject) {
		if (null == typeOfObject) {
			return null;
		}
		return typeOfObject.getRawType();
	}

	/**
	 *
	 * @param typeOfObject
	 * @return
	 */
	public Type getOwnerType(ParameterizedType typeOfObject) {
		if (null == typeOfObject) {
			return null;
		}
		return typeOfObject.getOwnerType();
	}

	/**
	 *
	 * @param typeOfObject
	 * @return
	 */
	public Type[] getTypes(Type typeOfObject) {
		if (typeOfObject instanceof ParameterizedType) {
			return getTypes((ParameterizedType) typeOfObject);
		}
		if (typeOfObject instanceof GenericArrayType) {
			return getTypes((GenericArrayType) typeOfObject);
		}
		return null;
	}

	/**
	 *
	 * @param typeOfObject
	 * @return
	 */
	public Type getRawType(Type typeOfObject) {
		if (typeOfObject instanceof ParameterizedType) {
			return getRawType((ParameterizedType) typeOfObject);
		}
		return null;
	}

	/**
	 *
	 * @param typeOfObject
	 * @return
	 */
	public Type getOwnerType(Type typeOfObject) {
		if (typeOfObject instanceof ParameterizedType) {
			return getOwnerType((ParameterizedType) typeOfObject);
		}
		return null;
	}

	/**
	 *
	 * @param typeOfObject
	 * @return
	 */
	public Type[] getTypes(GenericArrayType typeOfObject) {
		if (null == typeOfObject) {
			return null;
		}
		return new Type[] { typeOfObject.getGenericComponentType() };
	}
}
